﻿using System;
using System.Data.SqlClient;

namespace SimpleAsynch
{
    public class Program
    {
        public static String ConnectionString;
        public static int Count;

        public static SqlCommand CommandOne;
        public static SqlCommand CommandTwo;
        public static SqlCommand CommandThree;
        //public static SqlCommand CommandFour;
        //public static SqlCommand CommandFive;
        public static IAsyncResult Result;
        public static SqlDataReader Reader;
        public static SqlConnection Connection;

        static int Main(string[] args)
        {
            Initialize();
            Connect();
            RunQueryOne();
            RunQueryTwo();
            return 1;
        }

        public delegate void ResponseDelegate(string result);

        public static event ResponseDelegate QueryOneResponse;
        public static event ResponseDelegate QueryTwoResponse;
        public static event ResponseDelegate QueryThreeResponse;
        //public static event ResponseDelegate QueryFourResponse;
        //public static event ResponseDelegate QueryFiveResponse;

        public static void Initialize()
        {
            QueryOneResponse += new ResponseDelegate(OnQueryOneResponse);
            QueryTwoResponse += new ResponseDelegate(OnQueryTwoResponse);
            QueryThreeResponse += new ResponseDelegate(QueryThreeResponse);
            //QueryFourResponse += new ResponseDelegate(QueryFourResponse);
            //QueryFiveResponse += new ResponseDelegate(QueryFiveResponse);
        }

        public static void Connect()
        {
            ConnectionString = @"data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Northwind;User Instance=true;Asynchronous Processing=true;MultipleActiveResultSets=True";
            Connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
            Connection.Open();
        }

        public static void RunQueryOne()
        {
            CommandOne = new SqlCommand("WAITFOR DELAY '00:00:03'; SELECT * FROM [Orders] Limit 10", Connection);
            Result = CommandOne.BeginExecuteReader((result) =>
            {
                if (QueryOneResponse != null)
                {
                    //SqlDataReader reader = CommandOne.EndExecuteReader(result);

                    //string resultString = "";
                    //while (reader.Read())
                    //{
                    //    resultString += (reader["OrderID"].ToString());
                    //}

                    QueryOneResponse("QueryOneResponse Here");
                }
            }, null);
        }

        public static void RunQueryTwo()
        {
            CommandTwo = new SqlCommand("SELECT * FROM [Orders] Limit 20", Connection);
            Result = CommandTwo.BeginExecuteReader((result) =>
            {
                if (QueryTwoResponse != null)
                {
                    //SqlDataReader reader = CommandTwo.EndExecuteReader(result);

                    //string resultString = "";
                    //while (reader.Read())
                    //{
                    //    resultString += (reader["OrderID"].ToString());
                    //}

                    QueryTwoResponse("QueryTwoResponse Here");
                }
            }, null);
        }

        public static void RunQueryThree()
        {
            CommandThree = new SqlCommand("SELECT * FROM [Orders] Limit 30", Connection);
            Result = CommandThree.BeginExecuteReader((result) =>
            {
                if (QueryThreeResponse != null)
                {
                    QueryThreeResponse("QueryThreeResponse Here");
                }
            }, null);
        }

        //public static void RunQueryFour()
        //{
        //    CommandFour = new SqlCommand("SELECT * FROM [Orders] Limit 40", Connection);
        //    Result = CommandFour.BeginExecuteReader((result) =>
        //    {
        //        if (QueryFourResponse != null)
        //        {
        //            QueryFourResponse("QueryFourResponse Here");
        //        }
        //    }, null);
        //}

        //public static void RunQueryFive()
        //{
        //    CommandFive = new SqlCommand("SELECT * FROM [Orders] Limit 50", Connection);
        //    Result = CommandFive.BeginExecuteReader((result) =>
        //    {
        //        if (QueryFiveResponse != null)
        //        {
        //            QueryFiveResponse("QueryFiveResponse Here");
        //        }
        //    }, null);
        //}

        public static void OnQueryOneResponse(string result)
        {
            Console.WriteLine(String.Format("Response One: {0}", result));
        }

        public static void OnQueryTwoResponse(string result)
        {
            Console.WriteLine(String.Format("Response Two: {0}", result));
        }
        public static void OnQueryThreeResponse(string result)
        {
            Console.WriteLine(String.Format("Response Three: {0}", result));
        }

        //public static void OnQueryFourResponse(string result)
        //{
        //    Console.WriteLine(String.Format("Response Four: {0}", result));
        //}
        //public static void OnQueryFiveResponse(string result)
        //{
        //    Console.WriteLine(String.Format("Response Five: {0}", result));
        //}
    }
}
